package jp.co.hcs.attendancemanagement.dialog.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jp.co.hcs.attendancemanagement.action.AMAction;
import jp.co.hcs.attendancemanagement.dialog.logic.WorkSearchLogic;
import jp.co.hcs.attendancemanagement.dialog.logic.dto.WorkDTO;
import net.sf.json.JSONArray;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * The Class WorkSearchAction.
 */
public class WorkSearchAction extends AMAction {

  public ActionForward executeImpl(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) throws Exception {

    String division = request.getParameter("division");
    String dept = request.getParameter("department");

    if (division == null || dept == null) {
      return null;
    }

    response.setContentType("text/json");

    WorkSearchLogic logic = new WorkSearchLogic();
    List<WorkDTO> listWorks = logic.searchWork(division, dept);
    JSONArray jsonArray = JSONArray.fromObject(listWorks);

    PrintWriter pw = response.getWriter();
    pw.write("{\"listWorks\":" + jsonArray.toString() + "}");
    pw.flush();
    pw.close();

    return null;
  }

}
